package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.solidfire.gson.Gson;
import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.TakeGoldAdapter;
import com.wnhz.work.jy.bean.TakeGoldBean;
import com.wnhz.work.jy.bean.TakeGoldDetailsBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---提金记录详情
 */
public class TakeGoldRecordDetailsActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private String orderId;
    private TextView tv_order_code;
    private TextView tv_time;
    private TextView tv_start;
    private TextView tv_name;
    private TextView tv_phone;
    private TextView tv_address;

    private LinearLayout linear;
    private ListView listView;

    private TakeGoldDetailsBean takeGoldBean;
    private List<TakeGoldBean> goodsInfo = new ArrayList<>();
    private TakeGoldAdapter mAdapter;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    tv_order_code.setText(takeGoldBean.getOrderNo());
                    tv_time.setText(takeGoldBean.getAddtime());
                    if ("0".equals(takeGoldBean.getOrderState())) {
                        tv_start.setText("待付款");
                    } else if ("1".equals(takeGoldBean.getOrderState())) {
                        tv_start.setText("待发货");
                    } else if ("2".equals(takeGoldBean.getOrderState())) {
                        tv_start.setText("待收货");
                    } else if ("3".equals(takeGoldBean.getOrderState())) {
                        tv_start.setText("待评价");
                    } else if ("4".equals(takeGoldBean.getOrderState())) {
                        tv_start.setText("交易完成");
                    } else if ("5".equals(takeGoldBean.getOrderState())) {
                        tv_start.setText("取消订单");
                    } else if ("6".equals(takeGoldBean.getOrderState())) {
                        tv_start.setText("退款");
                    }
                    tv_name.setText(takeGoldBean.getRealName());
                    tv_phone.setText(takeGoldBean.getMobile());
                    tv_address.setText(takeGoldBean.getProvince() + "-" + takeGoldBean.getCity() + "-" + takeGoldBean.getCounty() + "-" + takeGoldBean.getAddress());
                    mAdapter.notifyDataSetChanged();
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_gold_record_details);

        initData();
        initView();
    }

    private void initData() {
        orderId = getIntent().getStringExtra("orderId");
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("订单详情");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        tv_order_code = (TextView) findViewById(R.id.tv_order_code);//订单号
        tv_time = (TextView) findViewById(R.id.tv_time);//创建时间
        tv_start = (TextView) findViewById(R.id.tv_start);//订单状态
        tv_name = (TextView) findViewById(R.id.tv_name);//姓名
        tv_phone = (TextView) findViewById(R.id.tv_phone);//电话
        tv_address = (TextView) findViewById(R.id.tv_address);//地址

        showDialog();
        uploadByxUtils();
        linear = (LinearLayout) findViewById(R.id.ll_product_details);
        addListView(linear);
    }

    private void addListView(LinearLayout linear) {
        final View view = getLayoutInflater().inflate(R.layout.addorderlistview, null);
        listView = (ListView) view.findViewById(R.id.view_add_listview);
        mAdapter = new TakeGoldAdapter(TakeGoldRecordDetailsActivity.this, goodsInfo);
        listView.setAdapter(mAdapter);

        linear.addView(view);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            default:
                break;
        }
    }

    private static final String TAG = "TakeGoldRecordDetailsActivity";

    private void uploadByxUtils() {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        XUtil.Post(ConfigUtil.TAKEINFO, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("result");
                    takeGoldBean = new TakeGoldDetailsBean();
                    Gson gson = new Gson();
                    takeGoldBean = gson.fromJson(jsonObject.optString("takeDetail"), TakeGoldDetailsBean.class);
                    Log.e(TAG, "onSuccess: " + takeGoldBean.getGoodsInfo().size());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                goodsInfo.clear();
                goodsInfo.addAll(takeGoldBean.getGoodsInfo());
                mHandler.sendEmptyMessage(1);
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }
        });
    }
}
